package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 殃及池鱼<br/>
 * 攻击一个随机目标造成{0}%技能伤害，若暴击则降低敌方全员{1}%攻击力，持续{2}回合
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90127 extends CombatSkill {

	public COMBAT_SKILL_90127(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit target = action.target.getUnitRandomly();
		if (target != null) {
			ActionEffect eff = action.addTarget(target);
			CombatUtils.calcMagicAttack(eff, skill.getParameter(0) / GameStaticConfig.percentBase);
			if (eff.getResult() == ActionEffect.RESULT_CRIT) {
				List<CombatUnit> targets = action.target.all();
				for (int i = 0; i < targets.size(); i++) {
					target = targets.get(i);
					if (!target.isDied()) {
						action.addBuff(new BUFF(skill, target));
					}
				}
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		int oldAttackPower;
		public BUFF(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(), owner, (int) skill.getParameter(2));
			active = true;
			int power = (int) owner.getAttackPower();
			oldAttackPower = power;
			power -= power * skill.getParameter(1) / GameStaticConfig.percentBase;
			owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, power);
		}
		@Override
		public void roundEnd(Round round) {
			super.roundEnd(round);
			if (state == STATE_REMOVED) {
				owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, oldAttackPower);
			}
		}
		@Override
		@JSONField(serialize = false)
		public boolean isDebuff() {
			return true;
		}
	}

}
